package com.itany.nmms.interceptor;

import com.itany.nmms.entity.Staff;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 石小俊
 */
public class CheckLoginInterceptor implements HandlerInterceptor {

    /**
     * 在进入Handler请求处理核心业务逻辑之前执行
     * @param request
     * @param response
     * @param handler
     * @return true:放行，false:不放行
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 从session中获取员工信息
        Staff staff = (Staff) request.getSession().getAttribute("staff");
        if (staff == null) {
            // 如果没有,则表示尚未登录
            request.setAttribute("loginMsg", "您尚未登录,请先登录!");
            request.getRequestDispatcher("/showLogin").forward(request, response);
            return false;
        }
        return true;
    }
}
